package com.mayx.datax.output;

import cn.hutool.json.JSONUtil;
import com.mayx.datax.beans.Table;
import com.mayx.datax.configuration.Configuration;
import com.mayx.datax.helper.DataxJsonHelper;
import com.mayx.datax.helper.MysqlHelper;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @description: 从mysql读取数据到其他数据源的配置
 * @author: mayx
 * @create: 2023-05-11 17:50
 **/
public class FromMysql {
    public static void mysqlToHDFS() throws IOException {
        // 生成 HDFS 入方向配置文件
        if (Configuration.IMPORT_OUT_DIR != null &&
                !Configuration.IMPORT_OUT_DIR.equals("")) {
            MysqlHelper mysqlHelper = new MysqlHelper(
                    Configuration.MYSQL_URL_IMPORT,
                    Configuration.MYSQL_DATABASE_IMPORT,
                    Configuration.MYSQL_TABLES_IMPORT);
            DataxJsonHelper dataxJsonHelper = new DataxJsonHelper();

            // 获取迁移操作类型
            String migrationType = Configuration.IMPORT_MIGRATION_TYPE;

            // 创建父文件夹
            Files.createDirectories(Paths.get(Configuration.IMPORT_OUT_DIR));
            List<Table> tables = mysqlHelper.getTables();

            // 判断传入的表是否为分表，根据判断结果采用不同的处理策略
            if (Configuration.IS_SEPERATED_TABLES.equals("1")) {
                for (int i = 0; i < tables.size(); i++) {
                    Table table = tables.get(i);
                    dataxJsonHelper.setTable(table, i, migrationType);
                }
                dataxJsonHelper.setColumns(tables.get(0), migrationType);

                // 输出最终Json配置
                FileWriter inputWriter = new FileWriter(Configuration.IMPORT_OUT_DIR + "/" + Configuration.MYSQL_DATABASE_IMPORT + "." + tables.get(0).name() + ".json");
                // 格式化JSON字符串
                String jsonStr = JSONUtil.toJsonStr(dataxJsonHelper.getInputConfig(), 4);
                // 创建输出流，将其与File对象关联
                BufferedWriter bufferedWriter = new BufferedWriter(inputWriter);
                // 将格式化后的JSON字符串写入输出流中
                bufferedWriter.write(jsonStr);
                bufferedWriter.close();
                inputWriter.close();
            } else {
                for (Table table : tables) {
                    // 设置表信息
                    dataxJsonHelper.setTableAndColumns(table, 0, migrationType);

                    // 输出最终Json配置
                    FileWriter inputWriter = new FileWriter(Configuration.IMPORT_OUT_DIR + "/" + Configuration.MYSQL_DATABASE_IMPORT + "." + table.name() + ".json");
                    // 格式化JSON字符串
                    String jsonStr = JSONUtil.toJsonStr(dataxJsonHelper.getInputConfig(), 4);
                    // 创建输出流，将其与File对象关联
                    BufferedWriter bufferedWriter = new BufferedWriter(inputWriter);
                    // 将格式化后的JSON字符串写入输出流中
                    bufferedWriter.write(jsonStr);
                    bufferedWriter.close();
                    inputWriter.close();
                }
            }
        }
    }
}
